package cn.edu.ctbu.dp01.api;

import cn.edu.ctbu.dp01.constent.REnum;
import cn.edu.ctbu.dp01.entity.Teacher;
import cn.edu.ctbu.dp01.exception.RException;
import cn.edu.ctbu.dp01.service.TeacherService;
import cn.edu.ctbu.dp01.util.RUtil;
import cn.edu.ctbu.dp01.vo.QueryObj;
import cn.edu.ctbu.dp01.vo.R;
import org.springframework.data.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    public R<List<Teacher>> findAll(){
        List<Teacher> teachers = teacherService.getAll();
        return RUtil.success(teachers);
    }
    @GetMapping("/{id}")
    public R<Teacher> findById(@PathVariable int id){
        Teacher teacher=teacherService.findById(id);
        return RUtil.success(teacher);
    }
    // 添加学生
    @PostMapping("/add")
    public R<Teacher> add(Teacher teacher) {

        return RUtil.success(teacherService.add(teacher));
    }
     // 更新学生信息
     @PutMapping("/update")
     public R<Teacher> update(Teacher teacher) {
     return RUtil.success(teacherService.update(teacher));
     }

     // 删除学生
     @DeleteMapping("/delete/{id}")
     public R delete(@PathVariable Integer id) {
     teacherService.delete(id);
     return RUtil.success();
     }
     @GetMapping("/validateUser")
     public R validateTnoAndPassword(String tno,String password) throws Exception{
        return RUtil.success(teacherService.validateUsernameAndPassword(tno,password));
     }
     @PostMapping("/getbypage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> qObj){
        //按id排序
         Sort sort = Sort.by(Sort.Direction.DESC, "id");
         Integer pageIndex = 0;
         Integer pageSize = 10;
         if (qObj==null){
             //teacher为空就直接调用分页
             Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
             Page<Teacher> teachers = teacherService.getAll(pageable);
             return RUtil.success(teachers.getContent(),teachers.getTotalElements());
         }else{
             pageIndex = qObj.getPage()-1;
             pageSize = qObj.getLimit();
             if(qObj.getData() instanceof Teacher){
                 Teacher teacher = (Teacher) qObj.getData();
                 Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
                 ExampleMatcher matcher = ExampleMatcher.matching()
                         .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                         .withIgnoreNullValues();
                 Example<Teacher> example = Example.of(teacher, matcher);
                 Page<Teacher> teacherPage = teacherService.getAll(example,pageable);
                 return RUtil.success(teacherPage.getContent(),teacherPage.getTotalElements());
             }else {
                 throw new RException(REnum.QUERY_ERR);
             }
         }
     }
}
